/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package viev.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author viev
 */
@ManagedBean
@SessionScoped
public class RSSFeedManager {

    private static String appPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("\\");
    private static String categoryXMLPath = "categories.xml";
    private static String rssLinksXMLPath = "rsslinks.xml";

    public List<Category> getCategories() {
        List<Category> lst = new ArrayList<Category>();
        try {
            Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(appPath + categoryXMLPath);
            XPath xPath = XPathFactory.newInstance().newXPath();

            NodeList categories = (NodeList) xPath.evaluate("//categories/category", dom, XPathConstants.NODESET);
            for (int i = 0; i < categories.getLength(); i++) {
                Element category = (Element) categories.item(i);
                lst.add(new Category(Integer.parseInt(category.getElementsByTagName("categoryId").item(0).getTextContent()),
                        category.getElementsByTagName("categoryName").item(0).getTextContent()));
            }
        } catch (Exception e) {
            //
        }
        return lst;
    }
    private int categoryId = 1;

    public String setShowChanelsByCategory(int categoryId) {
        this.categoryId = categoryId;
        return "index?faces-redirect=true";
    }
    private List<Chanel> chanels;

    public List<Chanel> getChanels() {
        chanels = new ArrayList<Chanel>();
        try {
            Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(appPath + rssLinksXMLPath);
            XPath xPath = XPathFactory.newInstance().newXPath();

            NodeList lst = (NodeList) xPath.evaluate("//rsslinks/rsslink[categoryId=" + categoryId + "]", dom, XPathConstants.NODESET);
            for (int i = 0; i < lst.getLength(); i++) {
                Element chanel = (Element) lst.item(i);
                RSSFeedParser parser = new RSSFeedParser(chanel.getElementsByTagName("link").item(0).getTextContent());
                chanels.add(parser.readFeed());
            }
        } catch (Exception e) {
            //
        }
        return chanels;
    }

    public List<Item> getItems() {
        List<Item> lst = new ArrayList<Item>();
        for (Chanel chanel : chanels) {
            lst.addAll(chanel.getItems());
        }
        return lst;
    }
    private Item selectedItem;

    public String setShowPage(Item selectedItem) {
        this.selectedItem = selectedItem;
        return "page?faces-redirect=true";
    }

    
}
